package studio.banner.reference.util;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.web.multipart.MultipartFile;
import studio.banner.reference.entity.Document;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordUtil {
    /**
     * 按行读取Word
     *
     * @param file
     * @return
     * @throws IOException
     */
    public List<String> wordImport(MultipartFile file) throws IOException {
        List<String> wordContent = new ArrayList<>();
        /*文件为空，跳出*/
        if (file == null) {
            return wordContent;
        }
        /*创建Word*/
        XWPFDocument doc = new XWPFDocument(file.getInputStream());
        //遍历段落,逐行追加
        for (XWPFParagraph p : doc.getParagraphs()) {
            wordContent.add(p.getParagraphText());
        }
        return wordContent;
    }

    /**
     * 将多行解析成单行，并封装成Document集合
     *
     * @param wordList
     * @return
     */
    public List<Document> toDocContent(List<String> wordList) {
        List<Document> documentList = new ArrayList<>();
        String numRegex = "\\[\\d+\\]\\s+(.*)";
        Pattern pattern = Pattern.compile(numRegex);
        Matcher matcher;
        String docContent = "";
        for (String word : wordList) {
            Document document = new Document();
            matcher = pattern.matcher(word);
            if (matcher.matches()) {
                document.setDocContent(matcher.group(1));
            } else {
                document.setDocContent(document.getDocContent() + word);
            }
            documentList.add(document);
        }
        return documentList;
    }


    /**
     * 生成Doc文档
     *
     * @param documents
     * @return
     */
    public XWPFDocument generateDoc(List<Document> documents) {
        /*创建Word文件*/
        XWPFDocument doc = new XWPFDocument();
        /*新建一个段落*/
        XWPFParagraph p;
        /*创建段落文本*/
        XWPFRun r;
        int i = 1;
        String brackets;
        /*新建一个段落*/
        for (Document document : documents) {
            brackets = "[" + i + "] ";
            p = doc.createParagraph();
            r = p.createRun();
            r.setText(brackets + document.getDocContent());
        }
        return doc;
    }


    /**
     * 下载Doc
     *
     * @param response
     * @param thesisName
     * @param doc
     * @throws IOException
     */
    public void downloadFile(HttpServletResponse response, String thesisName, XWPFDocument doc) throws IOException {
        /*文件名*/
        String fileName = thesisName + ".doc";
        /*设置强制下载不打开*/
        response.setContentType("application/force-download");
        /*设置文件名*/
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        OutputStream os = response.getOutputStream();
        List<String> strings = new ArrayList<>();
        doc.write(os);
        os.close();
        System.out.println("下载完成");
    }
}
